﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GenericHtml
{
    public class KeyValuePage : System.Web.UI.Page, IKeyValue, ILoginInfo
    {
        public UserInfo LoginUser { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            if (!this.Request.IsAuthenticated)
            {
                this.Response.Redirect("~/admin/login.aspx");
                return;
            }

            this.LoginUser = Session["current_user"] as UserInfo;
            if (this.LoginUser == null)
            {
                // 登录失效 处理
                this.Response.Redirect("~/admin/login.aspx");
                return;
            }
            
            this.SetValue("UName", this.LoginUser.U_CName);

            base.OnLoad(e);
        }

        private Dictionary<string, string> table = new Dictionary<string, string>();

        public string GetValue(string key)
        {
            if (!table.ContainsKey(key))
                return string.Empty;
            return this.table[key];
        }

        public void SetValue<T>(string key, T value)
        {
            this.table[key] = value.ToString();
        }

        public void SetValue<T>(string key, T value, Func<T, string> toValue)
        {
            this.table[key] = toValue(value);
        }
    }
}